package com.cheetah.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author Administrator
 * @Date 2020/5/25
 * @Version V1.0
 * @Description:
 **/
@Component
public class RedisUtil {

    @Autowired
    private RedisTemplate redisTemplate;


    public void set(String key, String value){
        redisTemplate.opsForValue().set(key,value);
    }

    public void set(String key, long value){
        redisTemplate.opsForValue().set(key,value);
    }

    public void set(String key, String value, int seconds){
        redisTemplate.opsForValue().set(key,value,seconds, TimeUnit.SECONDS);
    }

    public String get(String key){
        return (String)redisTemplate.opsForValue().get(key);
    }

    public void hset(String key,String field,Object value){
        redisTemplate.opsForHash().put(key,field,value);
    }

    public void hmset(String key,Map map){
        redisTemplate.opsForHash().putAll(key,map);
    }

    public Object hget(String key,String field){
        return redisTemplate.opsForHash().get(key,field);
    }

    public Map hmget(String key){
        return redisTemplate.opsForHash().entries(key);
    }
}
